#!/bin/bash

#  echo "Usage: ./run.sh -s <Steps> -mt <Map-tasks> -rt <Reduce-tasks> -r <Environment> -cleanup <Cleanup-Option> -a <Combine-steps_1_2>"
#  echo "                    [-Steps        		val (0-5)]"
#  echo "                    [-Map-tasks    		val (1)]" 
#  echo "                    [-Reduce-tasks 		val (1)]" 
#  echo "                    [-Environment  		val (local)]"
#  echo "                    [-Cleanup			val (IF_SUCCESSFUL)]" 
#  echo "                    [-Combine-steps_1_2   	val (True/False)]" 
#  echo "Note: Values in brackets indicate default values for optional params"


cleanup="IF_SUCCESSFUL"
maptasks=1
reducetasks=1
env="local"
combinesteps="False"
steps=""

while [ "$*" ] ; do
  case $1 in
   -s)
    shift
    steps=$1
    shift
    ;;
     
   -mt)
     shift
     maptasks=$1
     shift
     ;;
          
   -rt)
     shift
     reducetasks=$1
     shift
     ;;
       
   -r)
     shift
     env=$1
     shift
     ;;
       
   -cleanup)
     shift
     cleanup=$1
     shift
     ;;

   -a)
     shift
     combinesteps=$1
     shift
     ;;

   *)
     echo "Usage: ./run.sh -s <Steps> -mt <Map-tasks> -rt <Reduce-tasks> -r <Environment> -cleanup <Cleanup-Option> -a <Combine-steps_1_2>"
     echo "                    [Steps                val (0-5)]"
     echo "                    [Map-tasks            val (No.of Map Tasks<Numeric_value>)]" 
     echo "                    [Reduce-tasks         val (No.of Reduce Tasks<Numberic_value)]" 
     echo "                    [Environment          val (local/hadoop)]"
     echo "                    [Cleanup              val (IF_SUCCESSFUL/NONE)]" 
     echo "                    [Combine-steps_1_2    val (False/True)]" 
     exit 1

   esac
done

echo "Building archive"
./build-archive.sh
echo

if [ "$combinesteps" = "True" ]
then
    if [ -z "$steps" ]
    then
       steps="0,1,3,4,5"
    else
        if [ "$steps" = "2" ]
        then
            steps="1"
        fi
    fi
else
    if [ -z "$steps" ]
    then
       steps="0-5"
    fi
fi

echo "Executing MapReduce Job with the following configurations"
echo "========================================================="
echo "Step(s) = $steps"
echo "Environment = $env"
echo "Map-tasks = $maptasks"
echo "Reduce-tasks = $reducetasks"
echo "Cleanup = $cleanup"
echo "Combine-Steps 1_2 = $combinesteps"
echo

python tool/run.py -s $steps -e $env -m $maptasks -r $reducetasks -c $cleanup -a $combinesteps

